package sixyz.service;

import sixyz.dao.UserDAO;
import sixyz.entity.User;
import sixyz.entity.value.Message;

import java.sql.SQLException;
import java.util.Objects;

public class UserService {

    private final UserDAO userDAO = UserDAO.getInstance();

    public Message userLogin(String account, String password) throws SQLException, ClassNotFoundException {
        Message message = new Message();
        User user = userDAO.queryUsersByAccount(account);
        // 登录成功和失败会回传不同内容的消息模型
        if (user == null) {
            message.setStatusCode(0);
            message.setDescription("用户不存在");
        } else if (Objects.equals(password, user.getPassword())) {
            message.setDescription("登陆成功");
            message.setStatusCode(1);
            message.setObject(user);
        } else {
            message.setStatusCode(0);
            message.setDescription("密码错误");
        }
        return message;
    }

    public void userRegister(String account, String password, String nickname, String email) throws SQLException, ClassNotFoundException, IllegalAccessException {
        User user = new User(account, password, nickname, email);
        userDAO.insertUser(user);
    }

}
